/*****************************************************************************
* Keppe Messenger (KMP) Keppe Project-Team User Messaging System
* Copyright (C) 2013-2014 Keppe Project-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

#include "NotificationWidget.h"
#include "ui_NotificationWidget.h"
#include <QDesktopWidget>
#include <QCursor>
#include <QTimer>
#include <QDebug>
#include <QSize>

NotificationWidget::NotificationWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NotificationWidget)
{
    setWindowFlags(windowFlags()^Qt::WindowStaysOnTopHint^Qt::WindowSystemMenuHint^Qt::WindowTitleHint^Qt::FramelessWindowHint);
    alreadyClosed = false;
    noteColor = "blue";
    ui->setupUi(this);
    tn = new QTimer;
}

NotificationWidget::~NotificationWidget()
{
    delete ui;
}

void NotificationWidget::setNotificationPixmap(QPixmap pixmap)
{
    ui->labNotificationImage->setPixmap(pixmap);
}

void NotificationWidget::setNotificationTitle(QString title)
{
    this->setWindowTitle(title);
    ui->labNotificationTitle->setText(title);
}

void NotificationWidget::setNotificationText(QString text)
{
    ui->labNotificationText->setText(text);
}

void NotificationWidget::setNotificationBack(QString back)
{
    ret = back;
}

void NotificationWidget::setNotificationShow()
{
    QDesktopWidget dw;
    QSize desktopSize = dw.size();
    int dx = desktopSize.width() - this->width() - 5;
    int dy = desktopSize.height() - this->height() - 5;
    this->move(dx,dy);
    this->show();
    tn->singleShot(500,this,SLOT(checkSelection()));
    tn->singleShot(10000,this,SLOT(startDisableWindow()));
    tn->singleShot(500,this,SLOT(notificationColorTimer()));
}

void NotificationWidget::startDisableWindow()
{
    if (!alreadyClosed)
    {
        alreadyClosed = true;
        tn->singleShot(10,this,SLOT(disableWindowPart()));
    }
}

void NotificationWidget::disableWindowPart()
{
    if (windowOpacity() != 0)
    {
        setWindowOpacity(windowOpacity() - 0.02);
        tn->singleShot(10,this,SLOT(disableWindowPart()));
    }
    else
    {
        this->close();
    }
}

void NotificationWidget::checkSelection()
{
    bool isCloseSelected = ui->labClose->rect().contains(ui->labClose->mapFromGlobal(QCursor::pos()));
    if (oldCloseSelected != isCloseSelected)
    {
        if (isCloseSelected)
        {
            ui->labClose->setText("<img src=\":/gui/notification-closex.png\"/>");
        }
        else
        {
            ui->labClose->setText("<img src=\":/gui/notification-whitex.png\"/>");
        }
        oldCloseSelected = isCloseSelected;
    }
    bool isNoteSelected = false;
    if (ui->labNotificationImage->rect().contains(ui->labNotificationImage->mapFromGlobal(QCursor::pos())) || ui->labNotificationText->rect().contains(ui->labNotificationText->mapFromGlobal(QCursor::pos())) || ui->labNotificationSplit->rect().contains(ui->labNotificationSplit->mapFromGlobal(QCursor::pos()))) {isNoteSelected = true;}
    if (oldNoteSelected != isNoteSelected)
    {
        if (isNoteSelected)
        {
            ui->labNotificationImage->setStyleSheet("background-color: rgb(200, 200, 200);");
            ui->labNotificationText->setStyleSheet("background-color: rgb(200, 200, 200);");
            ui->labNotificationSplit->setStyleSheet("background-color: rgb(200, 200, 200);");
        }
        else
        {
            ui->labNotificationImage->setStyleSheet("");
            ui->labNotificationText->setStyleSheet("");
            ui->labNotificationSplit->setStyleSheet("");
        }
        oldNoteSelected = isNoteSelected;
    }
    tn->singleShot(10,this,SLOT(checkSelection()));
}

void NotificationWidget::closeEvent(QCloseEvent *ev)
{
    ev->accept();
    tn->deleteLater();
}

void NotificationWidget::on_labClose_mousePress(QMouseEvent *)
{
    if (!alreadyClosed)
    {
        alreadyClosed = true;
        tn->singleShot(10,this,SLOT(disableWindowPart()));
    }
}

void NotificationWidget::notificationColorTimer()
{
    if (noteColor == "blue")
    {
        ui->labNotificationTitle->setStyleSheet("background-color: rgb(255, 125, 0); color: rgb(255, 255, 255);");
        ui->labRandL->setStyleSheet("background-color: rgb(255, 125, 0);");
        ui->labRandR->setStyleSheet("background-color: rgb(255, 125, 0);");
        ui->labRandD->setStyleSheet("background-color: rgb(255, 125, 0);");
        ui->labClose->setStyleSheet("background-color: rgb(255, 125, 0);");
        noteColor = "orange";
    }
    else if (noteColor == "orange")
    {
        ui->labNotificationTitle->setStyleSheet("background-color: rgb(60, 60, 255); color: rgb(255, 255, 255);");
        ui->labRandL->setStyleSheet("background-color: rgb(60, 60, 255);");
        ui->labRandR->setStyleSheet("background-color: rgb(60, 60, 255);");
        ui->labRandD->setStyleSheet("background-color: rgb(60, 60, 255);");
        ui->labClose->setStyleSheet("background-color: rgb(60, 60, 255);");
        noteColor = "blue";
    }
    tn->singleShot(500,this,SLOT(notificationColorTimer()));
}

void NotificationWidget::on_labNotificationImage_mousePress(QMouseEvent *)
{
    notificationBack();
}

void NotificationWidget::on_labNotificationSplit_mousePress(QMouseEvent *)
{
    notificationBack();
}

void NotificationWidget::on_labNotificationText_mousePress(QMouseEvent *)
{
    notificationBack();
}

void NotificationWidget::notificationBack()
{
    emit this->notificationPressed(ret);
    if (!alreadyClosed)
    {
        alreadyClosed = true;
        tn->singleShot(10,this,SLOT(disableWindowPart()));
    }
}
